/**
 * 
 */
package org.wmine.data;

import java.io.File;
import java.io.IOException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**1
 * @author Rajeev
 *This class is to initialise appropriate objects to parse xml file  
 */
public class ParserXml {
	private DocumentBuilder parser;
	private Document xmlDocument;
	private ArrayList<Attribute> attributeCollection;
	private ArrayList<Attribute> tupleCollection;
	String xmlFilePath;
	
	public ParserXml(String xmlFilePath) throws ParserConfigurationException, SAXException, IOException {
		this.xmlFilePath = xmlFilePath; 
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder parser = factory.newDocumentBuilder();
		xmlDocument = (Document) parser.parse(new File(xmlFilePath));
		attributeCollection = new ArrayList<Attribute>();
		tupleCollection = new ArrayList<Attribute>();
		parseXML();
	}
	
	private void parseXML() {
		NodeList tableList= xmlDocument.getElementsByTagName("table");
		int noOfTables = tableList.getLength();
		//System.out.println(noOfTables);
		Table sampleTable = new Table();
		for (int i = 0 ; i < noOfTables ; i++){
			Element element = (Element) tableList.item(i);
			sampleTable.setNameTable(getElementFromXmlFile(element, "tablename"));
			//System.out.println(sampleTable.getNameTable());
			
			NodeList attributeList = xmlDocument.getElementsByTagName("attribute");
			int noOfAttributes = attributeList.getLength();
			sampleTable.setNoOfAttributes(noOfAttributes);
				System.out.println("Attrinutes are : "+sampleTable.getNoOfAttributes());
			for (int j = 0; j < noOfAttributes; j++) {
				Element elementA = (Element) attributeList.item(j);
				Attribute singleAttribute = new Attribute();
				singleAttribute.setAttributeName(getElementFromXmlFile(elementA,"name"));
				singleAttribute.setAttributeType(getElementFromXmlFile(elementA,"type"));
				System.out.println(singleAttribute.getAttributeName());
				System.out.println(singleAttribute.getAttributeType());
				attributeCollection.add(singleAttribute);				
			}
			sampleTable.setAttributeList(attributeCollection);
			
			NodeList tupleList = xmlDocument.getElementsByTagName("tuple");
			int noOfTuples = tupleList.getLength();
			sampleTable.setNoOfTuples(noOfTuples);
				System.out.println("no of tuples : "+sampleTable.getNoOfTuples());
			for (int j = 0; j < noOfTuples; j++) {
				Element elementT = (Element) tupleList.item(j);
				Tuple singleTuple = new Tuple();
				String rollNo = getElementFromXmlFile(elementT,"tuple");
				System.out.println( rollNo );
				//singleTuple.setTupleValuesList(tupleValuesList));
				
				//System.out.println(singleAttribute.getAttributeName());
												
			}				 
		}	
}
	
	public String getElementFromXmlFile(Element element, String tagName){
		NodeList temp = (NodeList) element.getElementsByTagName(tagName);
		Element subElement = (Element) temp.item(0);
		//Element subElement1 = (Element) temp.item(1);
		//System.out.println(subElement1.getFirstChild().getNodeValue());
		if (!subElement.getFirstChild().getNodeValue().equals(null))
		return subElement.getFirstChild().getNodeValue();
		else
		return null;
		}
		
	
	public static void main(String[] args) throws IOException {
		String xmlPath = "D:\\MScsem-III\\Project\\WMine\\xmldoc02.xml";
		try {
			ParserXml parser = new ParserXml(xmlPath);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
